﻿using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Config1
{
  internal class Program
  {
    static void Main(string[] args)
    {


      ServiceCollection services = new ServiceCollection();

      ConfigurationBuilder builder = new ConfigurationBuilder();
      //  builder.AddJsonFile("config.json", true, true); //从json中读取配置
      //builder.AddCommandLine(args);//从命令行中获取值
      builder.AddEnvironmentVariables("A1_"); //从环境变量中读取配置 A1_ 是前缀

      builder.Add(new FxConfigSource() { Path = "web.config" });


      IConfigurationRoot configRoot = builder.Build();
      //var a= configRoot.GetSection("").Value;
      services.AddOptions()
        .Configure<WebConfig>(e => configRoot.Bind(e))
        .Configure<Config>(e => configRoot.Bind(e))
        .Configure<MyAddress>(e => configRoot.GetSection("MyAddress").Bind(e));

      services.AddScoped<TestConfig>();
      services.AddScoped<Test2>();
      services.AddScoped<TestWebConfig>();

      using (var sp = services.BuildServiceProvider())
      {
        var wbc=sp.GetRequiredService<TestWebConfig>();
        wbc.Test();
        //while (true)
        //{
        //  using (var scope = sp.CreateScope())
        //  {
        //    var c = scope.ServiceProvider.GetRequiredService<TestConfig>();
        //    c.Test();

        //    var t2 = scope.ServiceProvider.GetRequiredService<Test2>();
        //    t2.Test();
        //  }
        //  Console.WriteLine("点击任意键继续");
        //  ConsoleKeyInfo key = Console.ReadKey();
        //  if (key.Key == ConsoleKey.A)
        //  {
        //    break;
        //  }
        //}

      }


      Console.ReadLine();
    }
  }
}
